package com.hyq.grid.controller;
import com.hyq.grid.algorithm.MyAlgo;
import com.hyq.grid.algorithm.TransformAlgo;
import com.hyq.grid.algorithm.gmsh;
import com.hyq.grid.algorithm.quadsTodelaunay;
import com.hyq.grid.common.File_operation;
import com.hyq.grid.common.gdalTool;
import com.hyq.grid.util.ProjectConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by HYQ on 2020/7/1 11:36
 */
@RestController
public class algorControl {

    @Value("${application.uploadpath}")
    private String upLoadPath;



    @RequestMapping("/getDelauney")
    public Map getdelaunay(@RequestBody Map<String, Object> border) throws Exception {
        List<Map> borderpoints = (List<Map>) border.get("border");
        gmsh mygmsh = new gmsh(borderpoints, Double.parseDouble(border.get("distance").toString()));
//        Map delatri = mygmsh.algo1("del2d");
        Map delatri = mygmsh.gmshAlgo("del2d");
        return delatri;
    }

    @RequestMapping("/getMeshAdapt")
    public Map getauto(@RequestBody Map<String, Object> border) throws Exception {

        List<Map> borderpoints = (List<Map>) border.get("border");
        gmsh mygmsh = new gmsh(borderpoints, Double.parseDouble(border.get("distance").toString()));
//        Map delatri = mygmsh.algo("meshadapt");
        Map delatri = mygmsh.gmshAlgo("meshadapt");
        return delatri;
    }

    @RequestMapping("/getauto")
    public Map getMeshAdapt(@RequestBody Map<String, Object> border) throws Exception {
        List<Map> borderpoints = (List<Map>) border.get("border");
        gmsh mygmsh = new gmsh(borderpoints, Double.parseDouble(border.get("distance").toString()));
//        Map delatri = mygmsh.algo("auto");
        Map delatri = mygmsh.gmshAlgo("auto");
        return delatri;
    }

    @RequestMapping("/getdelquad")
    public Map getdelquad(@RequestBody Map<String, Object> border) throws Exception {
        List<Map> borderpoints = (List<Map>) border.get("border");
        gmsh mygmsh = new gmsh(borderpoints, Double.parseDouble(border.get("distance").toString()));
        Map delatri = mygmsh.gmshAlgo("delquad");
        return delatri;
    }

    @RequestMapping("/getfront2d")
    public Map getfront2d(@RequestBody Map<String, Object> border) throws Exception {
        List<Map> borderpoints = (List<Map>) border.get("border");
        gmsh mygmsh = new gmsh(borderpoints, Double.parseDouble(border.get("distance").toString()));
//        Map delatri = mygmsh.algo("front2d");
        Map delatri = mygmsh.gmshAlgo("front2d");
        return delatri;
    }

    @RequestMapping("/getinitial2d")
    public Map getinitial2d(@RequestBody Map<String, Object> border) throws Exception {
        List<Map> borderpoints = (List<Map>) border.get("border");
        gmsh mygmsh = new gmsh(borderpoints, Double.parseDouble(border.get("distance").toString()));
//        Map delatri = mygmsh.algo("initial2d");
        Map delatri = mygmsh.gmshAlgo("initial2d");
        return delatri;
    }

    @RequestMapping("/getpack")
    public Map getpack(@RequestBody Map<String, Object> border) throws Exception {
        List<Map> borderpoints = (List<Map>) border.get("border");
        gmsh mygmsh = new gmsh(borderpoints, Double.parseDouble(border.get("distance").toString()));
//        Map delatri = mygmsh.algo("pack");
        Map delatri = mygmsh.gmshAlgo("pack");
        return delatri;
    }

    @RequestMapping("/getquads")
    public Map getquads(@RequestBody Map<String, Object> border) throws Exception {

        List<Map> borderpoints = (List<Map>) border.get("border");
        gmsh mygmsh = new gmsh(borderpoints, Double.parseDouble(border.get("distance").toString()));
        Map delatri = mygmsh.getquads();
        return delatri;
    }

    @RequestMapping("/quadsTodelaunay")
    public Map quadsTodelaunay(@RequestBody List<Map<String, Double>> points) throws Exception {
        quadsTodelaunay myquadtotri = new quadsTodelaunay(points);
        Map<String, Object> delatri = new HashMap<>();
        delatri.put("node", points);
        delatri.put("element", myquadtotri.quadsTodel());
        return delatri;
    }

    @RequestMapping("/gethexagon")
    public Map getHexagon(@RequestBody Map<String, Object> border) {
//        Map outPutDate = gdalTool.getDemInf("D:\\edgesDownload\\Data\\clip_out");
        List<Map> borderpoints = (List<Map>) border.get("border");
        Double distance = Double.parseDouble(border.get("distance").toString());
        MyAlgo myAlgo = new MyAlgo(borderpoints, distance);
//        Map outPutDate = myAlgo.hexagon();
        Map outPutDate = myAlgo.hexagon1();
        return outPutDate;
    }

    @RequestMapping("/getsquare")
    public Map getSquare(@RequestBody Map<String, Object> border) {
//        Map outPutDate = gdalTool.getDemInf("D:\\edgesDownload\\Data\\clip_out");
        List<Map> borderpoints = (List<Map>) border.get("border");
        Double distance = Double.parseDouble(border.get("distance").toString());
        MyAlgo myAlgo = new MyAlgo(borderpoints, distance);
        Map outPutDate = myAlgo.square();
        return outPutDate;
    }

    @RequestMapping("/gedeminf")
    public Map getDem() {
//        Map outPutDate = gdalTool.getDemInf("D:\\edgesDownload\\Data\\clip_out");
        Map outPutDate = gdalTool.getDemInf("E:\\Data\\ShapefileData\\Data\\clip_out");
        return outPutDate;
    }

    @RequestMapping("/demTohexagon")
    public Map demTohexagon() {

        Map outPutData = TransformAlgo.DemtoHexagon("E:\\Data\\ShapefileData\\Data\\clip_out");
//        Map outPutData = TransformAlgo.DemtoHexagon("E:\\Data\\ShapefileData\\DEM\\noname1");
//        Map outPutData = TransformAlgo.getDemTest();
        return outPutData;
    }

    @RequestMapping("/demToTri")
    public Map demToTri() throws Exception {

        Map outPutData = TransformAlgo.DemtoTri("E:\\Data\\ShapefileData\\Data\\clip_out");
//        Map outPutData = TransformAlgo.getDemTest();
        return outPutData;
    }

    @RequestMapping("/demtovoronoi")
    public Map demToVoronoi() throws Exception {
        System.out.println("******DEM转Voronoi******");
        Date date1 = new Date();
        Map outPutData = TransformAlgo.DemtoVoronoi("E:\\Data\\ShapefileData\\Data\\clip_out");
        Date date2 = new Date();
        System.out.println("计算时间：" + (date2.getTime() - date1.getTime()) / 1000.0 + "秒");
//        Map outPutData = TransformAlgo.getDemTest();
        return outPutData;
    }

    @RequestMapping("/getvoronoi")
    public Map getVoronoi(@RequestBody Map<String, Object> border) throws Exception {
//        Map outPutDate = gdalTool.getDemInf("D:\\edgesDownload\\Data\\clip_out");
        List<Map> borderpoints = (List<Map>) border.get("border");
        Double distance = Double.parseDouble(border.get("distance").toString());
        MyAlgo myAlgo = new MyAlgo(borderpoints, distance);

        Map voronoi = myAlgo.voronoi();
        return voronoi;
    }

    @PostMapping("/order/checkmore")
    public String checkMore(@RequestParam MultipartFile  file, @RequestParam String discount){

        String result = "amount:"+file.getName()+",discount:"+discount;

        return result;

    }


    @RequestMapping("/triToQuads")
    public Map triToQuads(@RequestBody String algo) throws Exception {

        Map outPutData = TransformAlgo.TriToQuads(algo);
//        Map outPutData = TransformAlgo.getDemTest();
        return outPutData;
    }

    @RequestMapping("/bacgrid")
    public List<Map> bacgrid(@RequestBody Map<String, Object> border) throws Exception {

        List<Map> borderpoints = (List<Map>) border.get("border");
        gmsh mygmsh = new gmsh(borderpoints, Double.parseDouble(border.get("distance").toString()));
        Map delatri = mygmsh.creatBacGrid(Double.parseDouble(border.get("distance").toString()));
        return (List<Map>) delatri.get("bacGrid");
    }

    @RequestMapping("/getDemInfor")
    public Map getDemInfor() {
        Map demInfor = gdalTool.getDemInf("E:\\Data\\ShapefileData\\Data\\clip_out");
//        Map demInfor = gdalTool.getDemInf("E:\\Data\\ShapefileData\\DEM\\noname1");
//                Map demInfor = gdalTool.getDemInf("E:\\Data\\ShapefileData\\Data\\dem.tif");
//        Map demInfor = gdalTool.getDemInf("E:\\arcmapPro\\dem");
        return demInfor;
    }

    @RequestMapping("/tesInter")
    public Map tesInter(@RequestBody Map<String, Object> border) throws Exception {
        List<Map> borderpoints = (List<Map>) border.get("border");
        gmsh mygmsh = new gmsh(borderpoints, Double.parseDouble(border.get("distance").toString()));
        Map delatri = mygmsh.gmshAlgo("del2d");
        return delatri;

    }

    @RequestMapping("/getSurUstrTri")
    public Map getSurUstrTri(@RequestBody Map<String, Object> postdata) throws Exception {
        System.out.println("***创建三维球面网格***");
        Double distance = Double.parseDouble(postdata.get("distance").toString());
        String algo = postdata.get("algo").toString();
        gmsh mygmsh = new gmsh(distance);
        Map mydata = mygmsh.gmsh3DAlgo(algo);
        return mydata;
    }

    @PostMapping("/upload")
    public Map<String, Object> fileUpLoad(MultipartFile file, HttpServletRequest req) {
        Map<String, Object> result = File_operation.upload(file,upLoadPath,req);
        return result;
    }

}
